package com.thb.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.entity.AgentPassage;
import com.thb.pay.mapper.AgentPassageMapper;
import com.thb.pay.service.AgentPassageService;
import com.thb.pay.vo.req.AgentPassagePageReq;
import com.thb.pay.vo.req.AgentPassageReq;
import com.thb.pay.vo.resp.AgentPassageVo;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 代理商-通道表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class AgentPassageServiceImpl extends BaseServiceImpl<AgentPassageMapper, AgentPassage> implements AgentPassageService {

    @Override
    public PageResult<AgentPassageVo> findPage(AgentPassagePageReq pageReq) {
        IPage<AgentPassage> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<AgentPassage> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, AgentPassageVo.class);
    }

    @Override
    public void add(AgentPassageReq agentPassageReq) {
        Assert.notNull(agentPassageReq, "agentPassageReq.not.null");
        Assert.notNull(agentPassageReq.getAgentId(), "agentId.not.null");
        Assert.notNull(agentPassageReq.getProductId(), "productId.not.null");
        AgentPassage agentPassage = new AgentPassage();
        BeanUtil.copyProperties(agentPassageReq, agentPassage);
        super.insert(agentPassage);
    }

    @Override
    public void update(AgentPassageReq agentPassageReq) {
        Assert.notNull(agentPassageReq, "agentPassageReq.not.null");
        Assert.notNull(agentPassageReq.getId(), "agentPassageId.not.null");
        AgentPassage agentPassage = super.selectById(agentPassageReq.getId());
        Assert.notNull(agentPassage, "agentPassage.not.null");
        AgentPassage targetAgentPassage = new AgentPassage();
        BeanUtil.copyProperties(agentPassageReq, targetAgentPassage);
        super.updateById(targetAgentPassage);
    }

    @Override
    public AgentPassageVo getById(Long id) {
        AgentPassage agentPassage = super.selectById(id);
        if (agentPassage != null) {
            AgentPassageVo agentPassageVo = new AgentPassageVo();
            BeanUtil.copyProperties(agentPassage, agentPassageVo);
            return agentPassageVo;
        }
        return null;
    }

    @Override
    public AgentPassageVo getByAgentIdAndProductId(Long agentId, Long productId) {
        LambdaQueryWrapper<AgentPassage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentPassage::getAgentId, agentId);
        queryWrapper.eq(AgentPassage::getProductId, productId);
        List<AgentPassage> resResultList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resResultList)) {
            AgentPassage agentPassage = resResultList.get(0);
            AgentPassageVo agentPassageVo = new AgentPassageVo();
            BeanUtil.copyProperties(agentPassage, agentPassageVo);
            return agentPassageVo;
        }
        return null;
    }

    @Override
    public List<AgentPassageVo> findAllList(AgentPassageReq conditionParam) {
        List<AgentPassageVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<AgentPassage> queryWrapper = new LambdaQueryWrapper<>();
        if (conditionParam != null) {
            if (conditionParam.getAgentId() != null) {
                queryWrapper.eq(AgentPassage::getAgentId, conditionParam.getAgentId());
            }
            if (conditionParam.getProductId() != null) {
                queryWrapper.eq(AgentPassage::getProductId, conditionParam.getProductId());
            }
            if (conditionParam.getStatus() != null && conditionParam.getStatus() != -99) {
                queryWrapper.eq(AgentPassage::getStatus, conditionParam.getStatus());
            }
        }
        List<AgentPassage> resResultList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resResultList)) {
            resResultList.forEach(agentPassage -> {
                AgentPassageVo agentPassageVo = new AgentPassageVo();
                BeanUtil.copyProperties(agentPassage, agentPassageVo);
                targetResultList.add(agentPassageVo);
            });
        }
        return targetResultList;
    }

    @Override
    public List<AgentPassageVo> findAllByAgentId(Long agentId) {
        List<AgentPassageVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<AgentPassage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentPassage::getAgentId, agentId);
        List<AgentPassage> resResultList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resResultList)) {
            resResultList.forEach(agentPassage -> {
                AgentPassageVo agentPassageVo = new AgentPassageVo();
                BeanUtil.copyProperties(agentPassage, agentPassageVo);
                targetResultList.add(agentPassageVo);
            });
        }
        return targetResultList;
    }
}
